(* Copyright 2007 dnAnalytics Project.
 *
 * Contributors to this file:
 * Jurgen Van Gael
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 *)
#light

namespace dnAnalytics.LinearAlgebra

open dnAnalytics.LinearAlgebra

/// A module which implements functional dense vector operations.
module DenseVector =

    /// Initialize a vector by calling a construction function for every element.
    /// <include file='../../../../FSharpExamples/DenseVector.xml' path='example'/> 
    let init (n: int) f =
        let v = new DenseVector(n)
        for i=0 to n-1 do
            v.[i] <- f i
        v
    
    /// Create a vector from a float list.
    let of_list (fl: float list) =
        let n = List.length fl
        let v = DenseVector(n)
        fl |> List.iteri (fun i f -> v.[i] <- f)
        v
    
    /// Create a vector from a sequences.
    let of_seq (fs: #seq<float>) =
        let n = Seq.length fs
        let v = DenseVector(n)
        fs |> Seq.iteri (fun i f -> v.[i] <- f)
        v
    
    /// Create a vector with evenly spaced entries: e.g. rangef -1.0 0.5 1.0 = [-1.0 -0.5 0.0 0.5 1.0]
    let rangef (start: float) (step: float) (stop: float) =
        let n = (int ((stop - start) / step)) + 1
        let v = new DenseVector(n)
        for i=0 to n-1 do
            v.[i] <- (float i) * step + start
        v
    
    /// Create a vector with integer entries in the given range.
    let range (start: int) (stop: int) =
        new DenseVector([| for i in [start .. stop] -> float i |])